//
//  ErgeNetSource.m
//  儿歌接口类
//
//  Created by 徐英杰 on 16/4/20.
//  Copyright © 2016年 Jeff. All rights reserved.
//

#import "ErgeNetSource.h"
#import <TFHpple.h>
@implementation ErgeNetSource





+(void)searchErgeWithPageUrl:(int)page
                successBlock:(void (^)(NSArray *resultList))successBlock
                   failBlock:(void (^)(NSError *error))failBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"http://list.youku.com/albumlist/show?id=23786645&ascending=1&page=%i.html",page];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
         if (!connectionError)
         {
             
             TFHpple *hpple = [TFHpple hppleWithHTMLData:data];
             
             
             NSString *xpath1 = @"//div/div/div/ul/li/a [@target='video']";
             NSString *xpath2 = @"//div/div/div/ul/li/img";
             
             NSArray *arr1 = [hpple searchWithXPathQuery:xpath1];
             NSArray *arr2 = [hpple searchWithXPathQuery:xpath2];
             
             NSMutableArray *mlist = [NSMutableArray array];
             
             NSMutableArray *list = [NSMutableArray array];
             for(TFHppleElement *tmp in arr2)
             {
                 
                 NSDictionary *dict = tmp.attributes;
                 [list addObject:dict[@"src"]];
             }
             for (int i=0; i<arr1.count; i++)
             {
                 
                 TFHppleElement *tmp = arr1[i];
                 NSDictionary *dict = tmp.attributes;
                 
                 [mlist addObject:@{@"name":dict[@"title"],@"img":list[i],@"url":dict[@"href"]}];
                 
             }
             successBlock(mlist);
         }
         else
         {
             failBlock(nil);
         }
         
     }];

}
@end
